<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>json</title>
	<!--<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>-->
</head>
<body>
<script>
	//JSON: JavaScript Object Notation
	//字符集:UTF-8
	//字符串用“”
	//Object也用“”
	//将JavaScript对象格式化成字符串
	let xiaoming = {
		name:'小明',
		age:14,
		gender:true,
		height:1.65,
		grade:null,
		'middle-school':'\"W3C\" Middle School',
		skills:['JavaScript','Java','Python']
	}
	let s = JSON.stringify(xiaoming);
	console.log("xiaoming:" + s);

	let s1 = JSON.stringify(xiaoming,null,' ');
	console.log("xiaoming:" + s1);

	let s2 = JSON.stringify(xiaoming,['name','skills'],' ');
	console.log("xiaoming:" + s2);

	//可以传入一个函数，这样对象的每个键值对都会被函数先处理
	function convert(key,value) {
		if (typeof value === 'string') {
			return value.toUpperCase();
		}
		return value;
	}
	let s3 = JSON.stringify(xiaoming,convert,' ');
	console.log("xiaoming:" + s3);

	//还可以精准控制如何序列化小明,xiaoming 定义toJson方法
	let xiaohong = {
		name:"小红",
		age:19,
		gender:true,
		'middle-school':'12 Middle School',
		skills:[
			'Python',
			'Php',
			'Java'
		],
		toJSON:function () {
			return {
				'Name':this.name,
				'Age':this.age
			}
		}
	}
	console.log("xiaohong:" + JSON.stringify(xiaohong));
	//将JSON格式的字符串,反序列化成JavaScript对象

	let reStr1 = "[1,2,3,true]";
	let reStr2 = '{"name":"小明","age":14}';
	let reStr3 = true;
	let reStr4 = '{"name":"小张","school":"1 Middle School","skills":["Python","Java","JavaScript"]}';

	jsonStr2Obj(reStr1);
	jsonStr2Obj(reStr2);
	jsonStr2Obj(reStr3);
	jsonStr2Obj(reStr4);

	function jsonStr2Obj(jsonStr) {
		let obj = JSON.parse(jsonStr);
		print(obj);
	}

	function print(obj) {
		console.log(obj);
	}

	let url = 'https://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20weather.forecast%20where%20woeid%20%3D%202151330&format=json'
</script>
</body>
</html>